package com.wlanauth.web.action;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.wlanauth.core.criteria.GeneralCriteria;
import com.wlanauth.core.criteria.GeneralCriteriaResult;
import com.wlanauth.domain.manage.UserUploadFile;
import com.wlanauth.service.manage.UserUploadFileService;
import com.wlanauth.web.core.JsonResult;
/**
 * 用户上载文件
 * @author zz
 * @date 2012-5-26
 * @email charles.zz.life@gmail.com
 */
public class UserFileAction extends com.wlanauth.web.core.file.FileUploadAction {
	
	private static final long serialVersionUID = 1L;
	
	private UserUploadFile entity;
	
	private UserUploadFileService userUploadFileService;
	
	private String downloadFilename;
	public String list(){
		GeneralCriteria criteria = this.buildWebQueryCriteria(UserUploadFile.class);
		
		if(entity!=null){
			if(entity.getBatchId()!=null){
				criteria.add(Restrictions.eq("batchId", entity.getBatchId()));
			}
			if(entity.getName()!=null){
				criteria.add(Restrictions.eq("name", entity.getName()));
			}
		}
		
		criteria.addOrder(Order.desc("uploadTime"));
		
		GeneralCriteriaResult<UserUploadFile> result = criteria.executeQuery();
		this.jsonResult = new JsonResult(this,result.getData(),result.getTotal());
		return this.actionJsonResult();
	}
	
	public String delete(){
		this.userUploadFileService.deleteByKey(this.entity.getId());
		this.jsonResult = new JsonResult(this,true);
		return this.actionJsonResult();
	}
	public UserUploadFile getEntity() {
		return entity;
	}
	
	public InputStream getDownloadInputStream() throws FileNotFoundException{
		UserUploadFile temp = this.userUploadFileService.get(entity.getId());
		downloadFilename = temp.getName();
		InputStream in = new FileInputStream(temp.getPath());
		return in;
	}
	public String download(){
		return SUCCESS;
	}
	public String getDownloadFilename() throws UnsupportedEncodingException{

		return new String(this.downloadFilename.getBytes("UTF-8"),"ISO-8859-1");
	}
	

	public void setEntity(UserUploadFile entity) {
		this.entity = entity;
	}

	public void setUserUploadFileService(UserUploadFileService userUploadFileService) {
		this.userUploadFileService = userUploadFileService;
	}
	
	
	
	
}
